package com.yango.member.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.commonutil.utils.JwtUtils;
import com.yango.commonutil.utils.MD5;
import com.yango.member.entity.Member;
import com.yango.member.entity.vo.LoginVo;
import com.yango.member.entity.vo.RegisterVo;
import com.yango.member.service.MemberService;
import com.yango.serviceutil.exception.YangoException;
import com.yango.serviceutil.vo.MemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-30
 */
@Api(description = "用户表 前端控制器")
@RestController
@RequestMapping("/memberservice/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    //用户登录-返回token字符串
    @ApiOperation(value = "用户登录-返回token字符串")
    @PostMapping("/login")
    public Result login(@ApiParam(name = "member", value = "登录对象", required = true)
                        @RequestBody Member member) {
        //调用方法登录
        String token = memberService.login(member);
        if (StringUtils.isEmpty(token)) {
            return Result.error().message("登录失败!用户名或密码错误,请重新登录!");
        } else {
            return Result.ok().message("登录成功!").data("token", token);
        }
    }

    //用户注册
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result register(@ApiParam(name = "registerVo", value = "注册对象", required = true)
                           @RequestBody RegisterVo registerVo) {
        //调用方法注册
        boolean flag = memberService.register(registerVo);
        //判断注册状态
        if (flag) {
            return Result.ok().message("注册成功!请登录!");
        } else {
            return Result.error().message("注册失败!请重新尝试!");
        }
    }

    //根据token获取登录信息
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/getLoginInfo")
    public Result getLoginInfo(HttpServletRequest request) {
        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //调用方法查询用户信息
        Member member = memberService.getById(memberId);
        return Result.ok().data("userInfo", member);

    }

    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息")
    @GetMapping("/getUserInfo/{userId}")
    public MemberVo getUserInfo(@ApiParam(name = "userId", value = "token", required = true)
                                @PathVariable String userId) {
        //根据id查询用户
        Member member = memberService.getById(userId);
        //将member对象的值复制到工具类中的memberVo中
        MemberVo memberVo = new MemberVo();
        BeanUtils.copyProperties(member, memberVo);
        return memberVo;
    }

    //统计网站每日注册人数
    @ApiOperation(value = "统计网站每日注册人数")
    @GetMapping("/getRegisterCount/{day}")
    public Result getRegisterCount(@ApiParam(name = "day", value = "统计日期", required = true)
                                   @PathVariable String day) {
        //根据日期查询注册人数
        Integer count = memberService.getRegisterCount(day);
        //返回
        return Result.ok().data("registerCount", count);
    }

    //统计网站每日登录人数
    @ApiOperation(value = "统计网站每日登录人数")
    @GetMapping("/getLoginCount/{day}")
    public Result getLoginCount(@ApiParam(name = "day", value = "统计日期", required = true)
                                @PathVariable String day) {
        //根据日期查询等人数
        Integer count = memberService.getLoginCount(day);
        //返回
        return Result.ok().data("loginCount", count);
    }

    //分页条件查询会员列表
    @ApiOperation(value = "分页条件查询会员列表")
    @PostMapping("/pageList/{current}/{limit}")
    public Result pageList(@ApiParam(name = "current", value = "当前页", required = true)
                           @PathVariable long current,
                           @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                           @PathVariable long limit,
                           @ApiParam(name = "memberVo", value = "查询对象", required = false)
                           @RequestBody com.yango.member.entity.vo.MemberVo memberVo) {
        //创建page对象
        Page<Member> page = new Page<>(current, limit);
        memberService.pageList(page, memberVo);
        List<Member> records = page.getRecords();
        long total = page.getTotal();
        return Result.ok().data("total", total).data("items", records);
    }

    //新增会员
    @ApiOperation(value = "新增会员")
    @PostMapping("/saveMember")
    public Result saveMember(@ApiParam(name = "member", value = "会员对象", required = true)
                             @RequestBody Member member) {
        boolean flag = memberService.saveMember(member);
        if (flag) {
            return Result.ok().message("添加会员成功!");
        } else {
            return Result.error().message("添加会员失败!");
        }
    }

    //修改会员
    @ApiOperation(value = "修改会员")
    @PutMapping("/updateMember")
    public Result updateMember(@ApiParam(name = "member", value = "会员对象", required = true)
                               @RequestBody Member member) {
        boolean flag = memberService.updateById(member);
        if (flag) {
            return Result.ok().message("修改会员成功!");
        } else {
            return Result.error().message("修改会员失败!");
        }
    }

    //根据id查询会员
    @ApiOperation(value = "根据id查询会员")
    @GetMapping("/getMemberById/{id}")
    public Result getMemberById(@ApiParam(name = "id", value = "会员id", required = true)
                                @PathVariable String id) {
        Member member = memberService.getById(id);
        System.out.println("++++++++++++++++"+member);
        System.out.println("--------------"+id);
        return Result.ok().data("member", member);
    }

    //根据id删除会员
    @ApiOperation(value = "删除会员")
    @DeleteMapping("/removeMemberById/{id}")
    public Result removeMemberById(@ApiParam(name = "id", value = "会员id", required = true)
                                   @PathVariable String id) {
        boolean remove = memberService.removeById(id);
        if (remove) {
            return Result.ok().message("删除会员成功!");
        } else {
            return Result.error().message("删除会员失败!");
        }
    }

}

